package controllers

import (
	"api/models"
	beego "github.com/beego/beego/v2/server/web"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.Data["Website"] = "beego.vip"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"
}

func (c *MainController) List() {
	uid, _ := c.GetInt("uid")

	//两表联查
	var orderListTwo []models.OrderDataTwo
	models.DB.Table("orders").
		Select("users.nickname,users.phone,orders.*").
		Joins("left join users on users.id=orders.uid").
		Where("uid=?", uid).Debug().
		Limit(2).
		Find(&orderListTwo)

	//三表联查
	var orderListThree []models.OrderDataThree
	models.DB.Table("orders").
		Select("goods.title,users.nickname,users.phone,orders.*").
		Joins("left join users on users.id=orders.uid").
		Joins("left join goods on orders.gid=goods.id").
		Where("uid=?", uid).Debug().
		Limit(2).
		Find(&orderListThree)

	c.Data["json"] = map[string]interface{}{
		"code":      200,
		"msg":       "success",
		"dataTwo":   orderListTwo,
		"dataThree": orderListThree,
	}
	c.ServeJSON()
	return
}
